package cn.com.dbappsecurity.utils;

import java.security.MessageDigest;

public class SecretUtil {

	private SecretUtil(){
		
	}
	
	/**
	 * 将文件进行 sha1 摘要
	 * @param str
	 * @return
	 * @throws Exception
	 */
	public static String digestSha1(String str) throws Exception{
		MessageDigest md = MessageDigest.getInstance("sha1");//md5
		md.update(str.getBytes());
		return toHexString(md.digest());
	}
	
	/*
	 * 转 byte[] 为16进制字符串
	 * @param pb
	 * @return
	 */
	private static String toHexString(byte[] pb){
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < pb.length; i++) {
			int val = pb[i] & 0xff;
			if (val < 0x10) {
				sb.append('0');
			}
			sb.append(Integer.toHexString(val));
		}
		return sb.toString();
	}
	
}
